/**
 * Created by yijx on 2017/6/13.
 */
angular.module("Speed", [])
    .controller("SpeedCtrl", ["$scope", "$http", function ($scope, $http) {

        $scope.bo = {
            textValue: "0",
            textareaRow: 5
        };

        $scope.config = {
            mysqlUrl: "",// mysql数据库地址
            mysqlName: "", // mysql数据库名称
            mysqlUser: "", //数据库用户名
            mysqlPassword: "" ,//数据库密码
        };

        $scope.control = {
            configEdit:false
        };

        $scope.boTemplateInfo = {};

        $scope.bo.onfocus = function () {
            $scope.bo.textareaRow = 20;
        };
        $scope.bo.onblur = function () {
            $scope.bo.textareaRow = 2;
        };

        loadConfig();


        function loadConfig() {
            $http({
                method: "get",
                url: "loadConfig"
            }).success(function (data, header, config, status) {
                //响应成功
                if (data.data = "success") {
                    // $scope.control.configEdit = true;
                    $scope.config.mysqlUrl = data.mysqlUrl;
                    $scope.config.mysqlName = data.mysqlName;
                    $scope.config.mysqlUser = data.mysqlUser;
                    $scope.config.mysqlPassword = data.mysqlPassword;
                }
            }).error(function (data, header, config, status) {
                alert("配置信息加载异常！");
            });
        }

        $scope.testConnection = function () {
            dbConnect().then(function (result) {
                if(result.message){
                    alert(result.message);
                }
                alert("数据库连接成功")
            },function (err) {
                alert(err);
            })
        };

        //测试连接
        function dbConnect(){
            return new Promise(function (resolve,reject) {
                //ip检查
                var mysqlUrl = $scope.config.mysqlUrl;
                if (!ipvalidate(mysqlUrl)) {
                    reject("数据库地址异常，请检查！");
                }
                $http({
                    method: "post",
                    params: {
                        mysqlUrl: $scope.config.mysqlUrl,
                        mysqlName: $scope.config.mysqlName,
                        mysqlUser: $scope.config.mysqlUser,
                        mysqlPassword: $scope.config.mysqlPassword
                    },
                    url: "testDB",
                    timeout:3000
                }).then(function (result) {
                    if (result.data && (result.data.result == "success" ||result.data.result == "created")) {
                        resolve(result.data);
                    }else{
                        reject(result.data.message);
                    }
                },function (err) {
                    reject(err);
                });
            })
        };

        //修改打印服务配置
        $scope.editConfig = function () {
            $scope.control.configEdit = false;
        };
        //保存打印服务配置
        $scope.saveConfig = function () {
            for (x in $scope.config) {
                if ($scope.config[x] == null) {
                    alert("所有配置必须填写！");
                    return;
                }
            }
            var connectResult;
            dbConnect().then(function (result) {
                connectResult = result;
                return $http({
                    method: "post",
                    params: {
                        print_token: $scope.config.print_token,
                        tenantId: $scope.config.tenantId,
                        mysqlUrl: $scope.config.mysqlUrl,
                        mysqlName: $scope.config.mysqlName,
                        mysqlUser: $scope.config.mysqlUser,
                        mysqlPassword: $scope.config.mysqlPassword,
                        serviceUrl: $scope.config.serviceUrl
                    },
                    url: "saveConfig"
                });
            }).then(function (result) {
                if (result.data && result.data.result == "success") {
                    $scope.control.configEdit = true;
                    $scope.$apply();
                    var message = connectResult.message?"配置保存成功："+connectResult.message:"配置保存成功,修改数据库连接请重启服务！";
                    alert(message);
                }else{
                    alert("配置信息保存失败");
                }
            }).catch(function (err) {
                if(err){
                    alert("配置信息保存失败:"+err)
                }
                alert("配置信息保存失败，请检查！");
            }).finally(function () {
            });
        };


        function ipvalidate(ipInfo) {
            if (ipInfo.indexOf(":") == -1) {
                return false;
            }
            var strs = ipInfo.split(":");
            var ip = strs[0];
            var port = strs[1];
            var reg1 = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/;
            var reg2 = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;
            if (!reg1.test(ip) || !reg2.test(port)) {
                return false;
            }
            return true;
        }


    }
    ]);